package cn.com.kh.lark;

import com.lark.oapi.Client;
import com.lark.oapi.core.request.RequestOptions;
import com.lark.oapi.core.utils.Jsons;
import com.lark.oapi.service.contact.v3.model.BatchGetIdUserReq;
import com.lark.oapi.service.contact.v3.model.BatchGetIdUserReqBody;
import com.lark.oapi.service.contact.v3.model.BatchGetIdUserResp;

// SDK 使用文档：https://github.com/larksuite/oapi-sdk-java/tree/v2_main
// 开发者复制该Demo后，需要修改Demo里面的"appID", "appSecret"为自己应用的appId,appSecret.
public class getOpenID {

    public static void main(String arg[]) throws Exception {
        // 构建client
        Client client = Client.newBuilder("appId", "appSecret")
                .disableTokenCache() //如需SDK自动管理租户Token的获取与刷新,可删除该行
                .build();

        // 创建请求对象
        BatchGetIdUserReq req = BatchGetIdUserReq.newBuilder()
                .userIdType("open_id")
                .batchGetIdUserReqBody(BatchGetIdUserReqBody.newBuilder()
                        .emails(new String[] {
                                "zhangsan@z.com",
                                "lisi@a.com"
                        })
                        .mobiles(new String[] {
                                "13812345678",
                                "13812345679"
                        })
                        .build())
                .build();

        // 发起请求
        // 如开启了Sdk的token管理功能，就无需调用 RequestOptions.newBuilder().tenantAccessToken("t-xxx").build()来设置租户token了
        BatchGetIdUserResp resp = client.contact().user().batchGetId(req, RequestOptions.newBuilder()
                .tenantAccessToken("t-g1049khNNSL6HRXHM4Y77FY2RLSXYKCTNBVMWGDC")
                .build());

        // 处理服务端错误
        if(!resp.success()) {
            System.out.println(String.format("code:%s,msg:%s,reqId:%s", resp.getCode(), resp.getMsg(), resp.getRequestId()));
            return;
        }

        // 业务数据处理
        System.out.println(Jsons.DEFAULT.toJson(resp.getData()));
    }
}